# -*- coding: utf-8 -*-

#------------------------------------------------------------------------
# Reaps tickdata from the configured symbols to the specified database
#------------------------------------------------------------------------


import ConfigParser
import logging
import logging.config
import sys

from DBWrapper import DBWrapper
from ReaperController import ReaperController

#----------------------------------------------------------------
# Initialise the config
#----------------------------------------------------------------

CONFIG_FILE = './cfg/config.cfg'

def initialiseLogger(config):
    logcfg = config.get('Logging', 'config')
    logging.config.fileConfig(logcfg)
    return logging.getLogger()

def dbConnectionParams(config):
    return config.get('Database', 'testdb')    

def ibConnectionParams(config):
    return config._sections['IBGateway']


if __name__ == '__main__':
    config = ConfigParser.ConfigParser()
    config.read(CONFIG_FILE)
    
    logger   = initialiseLogger  (config)    
    dbparams = dbConnectionParams(config) 
    ibparams = ibConnectionParams(config)
    
    contracts  = DBWrapper(dbparams).getContracts()
    reaper     = ReaperController(dbparams, contracts, ibparams)            
    
    if reaper.start():
        sys.stdin.readline()  #run with this until we think of a better way to control the threads          
    
    reaper.stop()

    
